package com.hmdp.service.impl;

import com.hmdp.entity.ShopType;
import com.hmdp.mapper.ShopTypeMapper;
import com.hmdp.service.IShopTypeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hmdp.utils.RedisConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 虎哥
 * @since 2021-12-22
 */
@Service
public class ShopTypeServiceImpl extends ServiceImpl<ShopTypeMapper, ShopType> implements IShopTypeService {

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public List<ShopType> queryList() {
        List shopTypeList = redisTemplate.opsForList().range(RedisConstants.SHOP_TYPR_KEY, 0, -1);

        if (shopTypeList != null && !shopTypeList.isEmpty()) {
            return shopTypeList;
        }

        shopTypeList = query().orderByDesc("sort").list();

        redisTemplate.opsForList().leftPushAll(RedisConstants.SHOP_TYPR_KEY, shopTypeList);

        return shopTypeList;
    }
}
